<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>Document</title>
  <link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.css" rel="stylesheet" />
  <style>
    label {
      font-weight: normal;
    }

    .panel-body .glyphicon-remove {
      display: none;
    }

    .panel-body:hover .glyphicon-remove {
      display: inline-block
    }
  </style>
</head>

<body>
  <div class="container">
    <div class="row">
      <div class="col-xs-12 col-lg-8 col-lg-offset-2">
        <div class="row text-right">
          <div class="col-xs-12" style="padding-top: 20px">
            <form method="post" action="/api/logout">
              <button class="btn btn-danger">退出</button>
            </form>
          </div>
        </div>
        <hr>
        <div class="row">
          <h2 class="col-xs-6">记账本</h2>
          <h2 class="col-xs-6 text-right"><a href="/create" class="btn btn-primary">添加账单</a></h2>
        </div>
        <hr />
        <div class="accounts">
              
        </div>
      </div>
    </div>
  </div>
</body>
<script src="/js/jquery.min.js"></script>
<script>
  $(function(){
    $.get('/api/list',function(res){
      const {data} = res
      const html = data.map(item=>innerHTML(item)).join('')
      $('.accounts').html(html)
      $('._delBtn').click(function(){
        const id = $(this).data('id') 
        $.ajax({
          url:'/api/del',
          type:'delete',
          data:{id},
          success:function(res){
            console.log(res)
            location.href = '/success'
          }
        })
      }) 
    })
    
    function innerHTML(data){
      return `<div class="panel ${data.type === -1 ?'panel-danger':'panel-success'}">
              <div class="panel-heading">
               ${data.time}
              </div>
              <div class="panel-body">
                <div class="col-xs-6">
                  ${data.title}
                </div>
                <div class="col-xs-2 text-center">
                  <span class="label ${data.type === -1 ?'label-warning':'label-success'}"">
                    ${data.type === -1 ?'支出':'收入'}
                  </span>
                </div>
                <div class="col-xs-2 text-right">
                ${data.account}元
                </div>
                <div class="col-xs-2 text-right">
                  <span class="glyphicon glyphicon-remove _delBtn" data-id="${data._id}" aria-hidden="true"></span>
                </div>
              </div>
            </div>`
    }
  })
</script>

</html>